import router from './router/index'
import store from './store/index'
import  NProgress from "nprogress"
import "nprogress/nprogress.css"
import {getToken} from "@/utils/store";
import {RouteLocationRaw, RouteRecordRaw} from "vue-router";
router.beforeEach(async (to, from, next) => {
   NProgress.start()
    if(getToken()){
        if(!store.getters.userInfo){
            await  store.dispatch("setUserInfo")
        }
        if(to.path == '/login'){
            next("/")
        }else {
            if(store.getters.addRoutes.length>0){
                next()
                NProgress.done()
                return
            }
          const routes = await store.dispatch("GenerateRoutes")
            routes.forEach((route: RouteRecordRaw)=>{
                router.addRoute(route)
            })
           next({...to, replace:true})
            //next()
            NProgress.done()
        }
    }else {
        if(to.path!='/login'){
            next("/login")
        }else {
            next()
            NProgress.done()
        }
    }
})
